/*
 * Copyright (c) 2016 zhangcm. All Rights Reserved.
 */
package com.jnzc.ocr.rest.app.shop.controller.common;

import com.google.common.collect.Lists;
import com.jnzc.ocr.common.utils.BusinessUtils;
import com.jnzc.ocr.common.utils.PasswordUtils;
import com.jnzc.ocr.common.utils.ValidateUtils;
import com.jnzc.ocr.common.vo.UserVo;
import com.jnzc.ocr.dependencies.rest.base.BaseController;
import com.jnzc.ocr.rest.app.shop.model.common.CommonUserLoginResultModel;
import com.jnzc.ocr.rest.app.shop.model.common.CommonUserRegisterParamModel;
import com.jnzc.ocr.serv.shop.entity.ShopUserRela;
import com.jnzc.ocr.serv.system.api.SysCompanyApi;
import com.jnzc.ocr.serv.shop.api.ShopMainApi;
import com.jnzc.ocr.serv.shop.api.ShopUserApi;
import com.jnzc.ocr.serv.system.consts.SysCompanyConsts;
import com.jnzc.ocr.serv.shop.consts.ShopUserConsts;
import com.jnzc.ocr.serv.system.entity.SysCompany;
import com.jnzc.ocr.serv.shop.entity.ShopMain;
import com.jnzc.ocr.serv.shop.entity.ShopUser;
import com.jnzc.ocr.serv.shop.vo.ShopUserCreateVo;
import com.jnzc.ocr.serv.system.api.SysUserApi;
import com.jnzc.ocr.serv.system.consts.SysUserConsts;
import com.jnzc.ocr.serv.system.entity.SysUser;
import com.jnzc.ocr.serv.third.api.CaptchaApi;
import io.swagger.annotations.*;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zhangcm
 * @version 1.0, 2017/8/29
 */
@Api(tags = {"common - 用户接口"})
@RestController
@RequestMapping("/common/user")
public class CommonUserController extends BaseController {

    private static final List<Integer> SHOP_TYPE_LIST = Lists.newArrayList(SysCompanyConsts.TYPE_SINGLE);

    @Autowired
    private SysUserApi sysUserApi;

    @Autowired
    private SysCompanyApi sysCompanyApi;

    @Autowired
    private ShopMainApi shopMainApi;

    @Autowired
    private ShopUserApi shopUserApi;

    @Autowired
    private CaptchaApi captchaApi;

    @ApiOperation(value = "登陆")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", required = true, paramType = "form"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, paramType = "form")
    })
    @PostMapping("/login")
    public CommonUserLoginResultModel login(@RequestParam String username, @RequestParam String password){
        ValidateUtils.notEmpty(username, "用户名");
        ValidateUtils.notEmpty(password, "密码");
        SysUser user = sysUserApi.findLevelOneByUsername(username, SysUserConsts.TYPE_SHOP);
        BusinessUtils.notNull(user, "用户名或密码不正确");
        if(!PasswordUtils.isEqual(user.getPassword(), password, user.getSalt())){
            BusinessUtils.throwException("用户名或密码不正确");
        }
        List<ShopUserRela> shopRelaList = shopUserApi.listShopRela(user.getId());
        if(CollectionUtils.isEmpty(shopRelaList) || shopRelaList.size() > 1){
            BusinessUtils.throwException("店铺信息不正确");
        }
        ShopUserRela shopRela = shopRelaList.get(0);

        UserVo userVo = new UserVo();
        userVo.setId(user.getId());
        userVo.setName(user.getUsername());
        userVo.setCompanyId(user.getCompanyId());
        userVo.setShopId(shopRela.getShopId());
        String token = super.create(userVo);
        // 返回结果
        CommonUserLoginResultModel result = new CommonUserLoginResultModel();
        result.setToken(token);
        return result;
    }

    @ApiOperation(value = "注销")
    @PostMapping("/logout")
    public void logout(){
        super.clear();
    }

    @ApiOperation(value = "注册")
    @PostMapping("/register")
    public void register(@RequestBody CommonUserRegisterParamModel param){
        ValidateUtils.in(param.getShopType(), SHOP_TYPE_LIST, "店铺类型");
        ValidateUtils.notEmpty(param.getShopName(), 1, 20, "店铺名称");
        ValidateUtils.isMobile(param.getMobile());
        ValidateUtils.isPassword(param.getPassword(), "密码");
        ValidateUtils.notEmpty(param.getCaptcha(), "验证码");

        captchaApi.verify(param.getMobile(), param.getCaptcha());

        // 创建公司
        SysCompany companyParam = new SysCompany();
        companyParam.setName(param.getShopName());
        companyParam.setType(param.getShopType());
        SysCompany company = sysCompanyApi.create(companyParam);

        // 创建用户
        SysUser userParam = new SysUser();
        userParam.setCompanyId(company.getId());
        userParam.setIfAdmin(true);
        userParam.setUsername(param.getMobile());
        userParam.setPassword(param.getPassword());
        SysUser user = sysUserApi.createShopUser(userParam);

        // 创建店铺
        ShopMain shopMainParam = new ShopMain();
        shopMainParam.setCompanyId(company.getId());
        shopMainParam.setName(param.getShopName());
        shopMainParam.setProvinceName(param.getProvinceName());
        shopMainParam.setProvinceCode(param.getProvinceCode());
        shopMainParam.setCityName(param.getCityName());
        shopMainParam.setCityCode(param.getCityCode());
        ShopMain shopMain = shopMainApi.insert(shopMainParam);

        // 创建店铺用户
        ShopUserCreateVo shopUserCreateVo = new ShopUserCreateVo();
        ShopUser shopUserParam = new ShopUser();
        shopUserParam.setUserId(user.getId());
        shopUserParam.setName(param.getMobile());
        shopUserParam.setType(ShopUserConsts.TYPE_SHOPKEEPER);
        shopUserParam.setMobile(param.getMobile());
        shopUserCreateVo.setShopUser(shopUserParam);
        shopUserCreateVo.setShopMainList(Lists.newArrayList(shopMain));
        ShopUser shopUser = shopUserApi.create(shopUserCreateVo);
    }

    @ApiOperation(value = "忘记密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mobile", value = "手机号", required = true, paramType = "form"),
            @ApiImplicitParam(name = "newPassword", value = "新密码", required = true, paramType = "form"),
            @ApiImplicitParam(name = "captcha", value = "验证码", required = true, paramType = "form"),
    })
    @PostMapping("/forgetPassword")
    public void forgetPassword(String mobile, String newPassword, String captcha){
        ValidateUtils.isMobile(mobile);
        ValidateUtils.isPassword(newPassword, "新密码");
        ValidateUtils.notEmpty(captcha, "验证码");

        captchaApi.verify(mobile, captcha);

        SysUser user = sysUserApi.findLevelOneByUsername(mobile, SysUserConsts.TYPE_SHOP);
        BusinessUtils.notNull(user, "用户不存在");
        sysUserApi.updatePassword(user.getId(), newPassword);
    }
}
